from flask import Flask, request, jsonify
from flask import make_response
from service.todo_service import *
from utils.common import *

app = Flask(__name__)
HOST = get_config("host")
PORT = get_config("port")
DEBUG = get_config("debug")


@app.route('/')
def index():
    return """
    <h1>功能设计：</h1>
        <p>1、{POST} http://127.0.0.1:8888/todo/api/tasks:创建一个待办事务，需要json信息包含："description"</p>
        <p>2、{PUT} http://127.0.0.1:8888/todo/api/tasks/<id>：完成一个事务</p>
        <p>3、{GET} http://127.0.0.1:8888/todo/api/tasks：查看所有事务</p>
        <p>3、？http://127.0.0.1:8888/todo/api/tasks/split：分别列出所有完成和未完成的事务</p>
        <p>4、？complete_time='%Y-%m-%d %H:%M:%S'":列出该Time之前已经完成的事务</p>
        <p>5、？create_time='%Y-%m-%d %H:%M:%S'：列出该Time之前添加的事务</p>
"""


@app.route("/todo/api/tasks", methods=["POST"])
def insert():
    if not request.json or not 'description' in request.json:
        return no_access(400)
    return insert_todo_task(request.json["description"])


@app.route("/todo/api/tasks", methods=["GET"])
def select_all_tasks():
    complete_time = request.args.get("complete_time")
    create_time = request.args.get("create_time")
    if create_time is None:
        create_time = get_now_time()
    _check_time = check_time(create_time, complete_time)
    if _check_time is not True:
        return _check_time
    return select_from_time(create_time=create_time, complete_time=complete_time)


@app.route("/todo/api/tasks/id/<string:task_id>", methods=["GET"])
def select_id(task_id):
    return select_from_id(task_id)


@app.route("/todo/api/tasks/<string:split>", methods=["GET"])
def select(split):
    if split != "split":
        return JsonData().error(code="1005",
                                msg="Please check the method accessed,go to /todo/api/tasks/split?",
                                data=[])
    complete_time = request.args.get("complete_time")
    create_time = request.args.get("create_time")
    if create_time is None:
        create_time = get_now_time()
    _check_time = check_time(create_time, complete_time)
    if _check_time is not True:
        return _check_time
    return select_from_time_split(create_time=create_time, complete_time=complete_time)


@app.route("/todo/api/tasks/<string:task_id>", methods=["PUT"])
def done(task_id):
    return done_todo_task(task_id)


@app.errorhandler(400)
def bad_request(error):
    return make_response(JsonData().error("400", f"请求无效 {error}", []), 400)


@app.errorhandler(403)
def no_access(error):
    return make_response(JsonData().error("403", f"拒绝访问 {error}", []), 403)


@app.errorhandler(404)
def not_found(error):
    return make_response(JsonData().error("404", f"找不到您想要的资源 {error}", []), 404)


@app.errorhandler(405)
def not_allowed(error):
    return make_response(JsonData().error("405", f"资源被禁止 {error}", []), 405)


@app.errorhandler(500)
def server_error(error):
    return make_response(JsonData().error("500", f"服务器错误 {error}", []), 500)


def check_time(create_time, complete_time):
    if not is_valid_date(complete_time) or not is_valid_date(create_time):
        return JsonData().error(code="1006",
                                msg="format of time is error,should be '%Y-%m-%d %H:%M:%S' or '%Y-%m-%d'",
                                data=[])
    return True


def run():
    return app.run(host=HOST, port=PORT, debug=DEBUG)
